package algorithm.tree.binarytree.bfs;

import algorithm.tree.binarytree.base.TreeNode;
import algorithm.tree.binarytree.base.TreeNodeEvent;

import java.util.*;
import java.util.function.Consumer;

public class TreeNodeBFSNonRecursion {
    public static void traverse(TreeNode node, Consumer<TreeNode> consumer) {
        if (node != null) {
            Queue<TreeNode> queue = new ArrayDeque<>();
            queue.add(node);
            while (!queue.isEmpty()  ){
                TreeNode temp =  queue.peek();
                consumer.accept(temp);
                if(temp.getLeft()!=null){
                    queue.add(temp.getLeft());
                }
                if(temp.getRight()!=null){
                    queue.add(temp.getRight());
                }
            }
        }
    }

}
